using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Base;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Extra.Data.Extra.Class;
using Extra.Data.Management;
using Extra.ERP;
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using Common.Class;
using DevExpress.Utils;
using APP.HELPER.Data.Helper;
namespace Extra.Data
{
    public class xucDatabase : xucMain
    {
        private BandedGridColumn colCreated;
        private BandedGridColumn colDatabase;
        private BandedGridColumn colPath;
        private BandedGridColumn colSTT;
        private BandedGridColumn colVersion;
        private IContainer components = null;
        private BindingSource dataBindingSource;
        private dsData dsData;
        protected AdvBandedGridView gbList;
        protected GridControl gcList;
        private GridBand gridBand3;
        private string m_database;
        private RepositoryItemDateEdit repositoryItemDateEdit1;
        private BandedGridColumn STT;

        public event DatabasePercentEventHander DatabasePercent;

        public event DatabaseReloadEventHander DatabaseReload;

        public event DatabaseReloadCompleteEventHander DatabaseReloadComplete;

        public event DatabaseSelectedEventHander DatabaseSelected;

        public xucDatabase()
        {
            this.InitializeComponent();
        }

        public void Clear()
        {
            this.dsData.Data.Rows.Clear();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void gbList_Click(object sender, EventArgs e)
        {
            object cellValue = this.GetCellValue(this.gbList.FocusedRowHandle, "Database");
            if (cellValue != null)
            {
                this.m_database = cellValue.ToString();
                this.InvokeDatabaseSelected(cellValue.ToString());
            }
        }

        public object GetCellValue(RowClickEventArgs e)
        {
            AdvBandedGridView gbList = this.gbList;
            GridColumn column = new GridColumn();
            if (e.ColumnIndex != -1)
            {
                column = gbList.Columns[e.ColumnIndex];
            }
            else
            {
                column = gbList.Columns[e.FieldName];
            }
            return gbList.GetRowCellValue(e.RowIndex, column);
        }

        public object GetCellValue(int RowIndex, int ColumIndex)
        {
            return this.GetCellValue(new RowClickEventArgs(RowIndex, ColumIndex));
        }

        public object GetCellValue(int RowIndex, string FieldName)
        {
            return this.GetCellValue(new RowClickEventArgs(RowIndex, FieldName));
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            this.gcList = new GridControl();
            this.dataBindingSource = new BindingSource(this.components);
            this.dsData = new dsData();
            this.gbList = new AdvBandedGridView();
            this.gridBand3 = new GridBand();
            this.colSTT = new BandedGridColumn();
            this.colDatabase = new BandedGridColumn();
            this.colVersion = new BandedGridColumn();
            this.colCreated = new BandedGridColumn();
            this.repositoryItemDateEdit1 = new RepositoryItemDateEdit();
            this.colPath = new BandedGridColumn();
            this.STT = new BandedGridColumn();
            this.gcList.BeginInit();
            ((ISupportInitialize)this.dataBindingSource).BeginInit();
            this.dsData.BeginInit();
            this.gbList.BeginInit();
            this.repositoryItemDateEdit1.BeginInit();
            this.repositoryItemDateEdit1.VistaTimeProperties.BeginInit();
            base.SuspendLayout();
            this.gcList.DataSource = this.dataBindingSource;
            this.gcList.Dock = DockStyle.Fill;
            this.gcList.Location = new Point(0, 0);
            this.gcList.LookAndFeel.SkinName = "Office 2007 Black";
            this.gcList.MainView = this.gbList;
            this.gcList.Name = "gcList";
            this.gcList.RepositoryItems.AddRange(new RepositoryItem[] { this.repositoryItemDateEdit1 });
            this.gcList.Size = new Size(0x25d, 0x1cd);
            this.gcList.TabIndex = 0x25;
            this.gcList.ViewCollection.AddRange(new BaseView[] { this.gbList });
            this.dataBindingSource.DataMember = "Data";
            this.dataBindingSource.DataSource = this.dsData;
            this.dsData.DataSetName = "dsData";
            this.dsData.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema;
            this.gbList.Appearance.SelectedRow.GradientMode = LinearGradientMode.Vertical;
            this.gbList.AppearancePrint.FilterPanel.Options.UseFont = true;
            this.gbList.AppearancePrint.FilterPanel.Options.UseTextOptions = true;
            this.gbList.AppearancePrint.FilterPanel.TextOptions.Trimming = Trimming.Word;
            this.gbList.Bands.AddRange(new GridBand[] { this.gridBand3 });
            this.gbList.BorderStyle = BorderStyles.NoBorder;
            this.gbList.Columns.AddRange(new BandedGridColumn[] { this.colSTT, this.colDatabase, this.colVersion, this.colCreated, this.colPath });
            this.gbList.GridControl = this.gcList;
            this.gbList.Name = "gbList";
            this.gbList.OptionsBehavior.AllowIncrementalSearch = true;
            this.gbList.OptionsFilter.ShowAllTableValuesInFilterPopup = true;
            this.gbList.OptionsFilter.UseNewCustomFilterDialog = true;
            this.gbList.OptionsPrint.ExpandAllDetails = true;
            this.gbList.OptionsPrint.PrintDetails = true;
            this.gbList.OptionsPrint.PrintFilterInfo = true;
            this.gbList.OptionsSelection.MultiSelect = true;
            this.gbList.OptionsView.AllowHtmlDrawHeaders = true;
            this.gbList.OptionsView.AnimationType = GridAnimationType.AnimateFocusedItem;
            this.gbList.OptionsView.ColumnAutoWidth = true;
            this.gbList.OptionsView.ShowBands = false;
            this.gbList.OptionsView.ShowGroupPanel = false;
            this.gbList.ViewCaption = "Danh s\x00e1ch Nh\x00e2n vi\x00ean";
            this.gbList.Click += new EventHandler(this.gbList_Click);
            this.gridBand3.Columns.Add(this.colSTT);
            this.gridBand3.Columns.Add(this.colDatabase);
            this.gridBand3.Columns.Add(this.colVersion);
            this.gridBand3.Columns.Add(this.colCreated);
            this.gridBand3.Columns.Add(this.colPath);
            this.gridBand3.Name = "gridBand3";
            this.gridBand3.Width = 0x248;
            this.colSTT.AppearanceCell.Options.UseTextOptions = true;
            this.colSTT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            this.colSTT.AppearanceHeader.Options.UseTextOptions = true;
            this.colSTT.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            this.colSTT.FieldName = "STT";
            this.colSTT.Name = "colSTT";
            this.colSTT.OptionsColumn.AllowEdit = false;
            this.colSTT.OptionsColumn.FixedWidth = true;
            this.colSTT.OptionsColumn.ReadOnly = true;
            this.colSTT.Visible = true;
            this.colSTT.Width = 0x26;
            this.colDatabase.Caption = "Cơ Sở Dữ Liệu";
            this.colDatabase.FieldName = "Database";
            this.colDatabase.Name = "colDatabase";
            this.colDatabase.OptionsColumn.AllowEdit = false;
            this.colDatabase.OptionsColumn.FixedWidth = true;
            this.colDatabase.OptionsColumn.ReadOnly = true;
            this.colDatabase.Visible = true;
            this.colDatabase.Width = 130;
            this.colVersion.Caption = "Phi\x00ean Bản";
            this.colVersion.FieldName = "Version";
            this.colVersion.Name = "colVersion";
            this.colVersion.OptionsColumn.AllowEdit = false;
            this.colVersion.OptionsColumn.FixedWidth = true;
            this.colVersion.OptionsColumn.ReadOnly = true;
            this.colVersion.Visible = true;
            this.colVersion.Width = 70;
            this.colCreated.Caption = "Ng\x00e0y Tạo";
            this.colCreated.ColumnEdit = this.repositoryItemDateEdit1;
            this.colCreated.FieldName = "Created";
            this.colCreated.Name = "colCreated";
            this.colCreated.OptionsColumn.AllowEdit = false;
            this.colCreated.OptionsColumn.FixedWidth = true;
            this.colCreated.OptionsColumn.ReadOnly = true;
            this.colCreated.Visible = true;
            this.colCreated.Width = 130;
            this.repositoryItemDateEdit1.AutoHeight = false;
            this.repositoryItemDateEdit1.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.repositoryItemDateEdit1.DisplayFormat.FormatString = "{0:dd/MM/yyyy}";
            this.repositoryItemDateEdit1.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            this.repositoryItemDateEdit1.EditFormat.FormatString = "{0:dd/MM/yyyy}";
            this.repositoryItemDateEdit1.EditFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            this.repositoryItemDateEdit1.Name = "repositoryItemDateEdit1";
            this.repositoryItemDateEdit1.VistaTimeProperties.Buttons.AddRange(new EditorButton[] { new EditorButton() });
            this.colPath.Caption = "Đường Dẫn";
            this.colPath.FieldName = "Path";
            this.colPath.Name = "colPath";
            this.colPath.OptionsColumn.AllowEdit = false;
            this.colPath.OptionsColumn.ReadOnly = true;
            this.colPath.Visible = true;
            this.colPath.Width = 0xd8;
            this.STT.Caption = "STT";
            this.STT.Name = "STT";
            this.STT.Visible = true;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.gcList);
            base.Name = "xucDatabase";
            base.Size = new Size(0x25d, 0x1cd);
            this.gcList.EndInit();
            ((ISupportInitialize)this.dataBindingSource).EndInit();
            this.dsData.EndInit();
            this.gbList.EndInit();
            this.repositoryItemDateEdit1.VistaTimeProperties.EndInit();
            this.repositoryItemDateEdit1.EndInit();
            base.ResumeLayout(false);
        }

        private void InvokeDatabasePercent(int e)
        {
            DatabasePercentEventHander databasePercent = this.DatabasePercent;
            if (databasePercent != null)
            {
                databasePercent(this, e);
            }
        }

        private void InvokeDatabaseSelected(string db)
        {
            DatabaseSelectedEventHander databaseSelected = this.DatabaseSelected;
            if (databaseSelected != null)
            {
                databaseSelected(this, db);
            }
        }

        private void RaiseDatabaseReloadCompleteEventHander(DataTable dt)
        {
            if (this.DatabaseReloadComplete != null)
            {
                this.DatabaseReloadComplete(this, dt);
            }
        }

        private void RaiseDatabaseReloadEventHander(int Percent)
        {
            if (this.DatabaseReload != null)
            {
                this.DatabaseReload(this, Percent);
            }
        }

        public override void Refesh(string ConnectString)
        {
            this.dsData.Data.Rows.Clear();
            SqlConnection sqlConnection = new SqlConnection(ConnectString);
            sqlConnection.Open();
            ServerConnection serverConnection = new ServerConnection(sqlConnection);
            Server server = new Server(serverConnection);
            DataTable dt = new DataTable();
            dt.Columns.Add("STT");
            dt.Columns.Add("Database");
            dt.Columns.Add("Version");
            dt.Columns.Add("Created");
            dt.Columns.Add("Path");
            this.RaiseDatabaseReloadEventHander(0);
            SqlHelper.ConnectString = ConnectString;
            foreach (Microsoft.SqlServer.Management.Smo.Database database in server.Databases)
            {
                if (!database.IsSystemObject)
                {
                    try
                    {
                        SYS_INFO sys_info = new SYS_INFO();
                        if (sys_info.GetInfo(database.Name) == "OK")
                        {
                            object[] values = new object[] { dt.Rows.Count + 1, database.Name, sys_info.Version, database.CreateDate, database.PrimaryFilePath };
                            DATA data = new DATA(database.Name, sys_info.Version, database.CreateDate, database.PrimaryFilePath);
                            dt.Rows.Add(values);
                        }
                    }
                    catch
                    {
                    }
                }
            }
            this.RaiseDatabaseReloadCompleteEventHander(dt);
        }

        public void Set(DataTable dt)
        {
            this.gcList.DataSource = null;
            this.gcList.DataSource = dt;
        }

        public void SetData(DATA item)
        {
            this.dsData.Data.AddDataRow(this.dsData.Data.Rows.Count + 1, item.Database, item.Version, item.Created, item.Path);
        }

        public void SetData(DataTable dt)
        {
            this.gbList.SelectAll();
            this.gbList.DeleteSelectedRows();
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    AdvBandedGridView gbList = this.gbList;
                    int focusedRowHandle = gbList.FocusedRowHandle;
                    gbList.AddNewRow();
                    focusedRowHandle = gbList.FocusedRowHandle;
                    gbList.SetRowCellValue(focusedRowHandle, "STT", this.gbList.RowCount + 1);
                    gbList.SetRowCellValue(focusedRowHandle, "Database", dt.Rows[i]["Database"]);
                    gbList.SetRowCellValue(focusedRowHandle, "Version", dt.Rows[i]["Version"]);
                    gbList.SetRowCellValue(focusedRowHandle, "Created", dt.Rows[i]["Created"]);
                    gbList.SetRowCellValue(focusedRowHandle, "Path", dt.Rows[i]["Path"]);
                    gbList.UpdateCurrentRow();
                }
            }
        }

        public string Database
        {
            get
            {
                object cellValue = this.GetCellValue(this.gbList.FocusedRowHandle, "Database");
                if (cellValue == null)
                {
                    XtraMessageBox.Show(this, "Lỗi:\n\n\tKh\x00f4ng thực hiện được!", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return null;
                }
                this.m_database = cellValue.ToString();
                return this.m_database;
            }
        }

        public delegate void DatabaseSelectedEventHander(object sender, string db);
    }

}
